<template>
  <div>
            <div>
              <el-dialog :title="form._id ? '修改' : '新增'" :visible.sync="dialogFormVisible" width="400px">
                <el-form :model="form" ref="form" :rules="rules" label-width="100px">
                  <el-form-item label="图片" prop="img">
                    <!-- <el-input type="text" v-model="form.img"></el-input> -->
                    <input type="file"/>
                  </el-form-item>
                  <el-form-item label="标题" prop="title">
                    <el-input type="text" v-model="form.title"></el-input>
                  </el-form-item>
                  <el-form-item label="跳转地址" prop="url">
                    <el-input type="text" v-model="form.url"></el-input>
                  </el-form-item>
                  <el-form-item label="类型" prop="type">
                    <el-select v-model="form.type" placeholder="请选择">
                      <el-option
                        v-for="type in types"
                        :key="type.value"
                        :label="type.label"
                        :value="type.value"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogFormVisible = false">取 消</el-button>
                  <el-button type="primary" @click="submit">确 定</el-button>
                </div>
              </el-dialog>  
            </div>

                     
  </div>

  
  
</template>

<script>

export default {
  name: 'shouyeCategoryEdit',
  components: {},
  data(){
    return {
      params:{
        title:'',
        type:'category',
        pageNum:1,
        pageSize:5
      },
      rows:[],
      total:0,
      selecteds:[]
    }
  },
  methods:{
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    //删除
    remove(row) {
                this.$http({
            url: "/admin/index",
            method: "DELETE",
            params:{indexId:row._id}
          }).then((res) => {
            if (!res.errorMsg) {
              this.getList(1);
            }
          });
    },
    //批量删除
    removeMore(){
           //获取选中的数据
      let checkedRowsPrmose = []
      let success = [],fail = []
      for(let i = 0;i<this.selecteds.length;i++){
        checkedRowsPrmose.push(
          new Promise(resovle => {
            let row = this.selecteds[i]
            this.$http({
            url: "/adminwen/news",
            method: "DELETE",
            params:{indexId:row._id}
          }).then((res) => {
            if (!res.errorMsg) {
              success.push(row._id)
            }else{
              fail.push(row._id)
            }
            resovle()
          });
          })
        )
      }
      this.$confirm('是否删除数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
              Promise.all(checkedRowsPrmose).then(() => {
        this.$message({
          message:`批量删除完成，成功删除${success.length}条数据`,
          duration:3000
        });
        this.getList(1)
        })
        })
    },
      submit(){
        this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.indexId = this.form._id
          this.$http({
            url: "/admin/index",
            method: this.form.indexId ? 'PUT' : 'POST',//根据indexId判断是新增还是修改
            data: this.form,
          }).then((res) => {
            if (!res.errorMsg) {
              this.dialogFormVisible = false;
              this.$emit('load',this.form.indexId ? undefined : 1)
            }
          });
        }
      });
    },
},
  created(){
  },
  mounted(){
    
  }
}
</script>

<style>

</style>